package com.fliter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
@WebFilter(urlPatterns = "/*", filterName = "loginFilter")
public class LoginFilter implements Filter {

	/**
     * 容器加载的时候调用
     */
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
         System.out.println("拦截器进入========拦截器进入========");
     }
     
     /**
      * 请求被拦截的时候进行调用
      */
     @Override
     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
         System.out.println("拦截中========拦截中========");

         HttpServletRequest hrequest = (HttpServletRequest)servletRequest;
         HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
         /*if(hrequest.getRequestURI().indexOf("/index") != -1 ||
                 hrequest.getRequestURI().indexOf("/api") != -1 ||
                 hrequest.getRequestURI().indexOf("/online") != -1 ||
                 hrequest.getRequestURI().indexOf("/login") != -1 ||
                 hrequest.getRequestURI().indexOf("/thymeleaf") != -1
                 ) {
             filterChain.doFilter(servletRequest, servletResponse);
         }else {
             wrapper.sendRedirect("/login");
         }*/
         filterChain.doFilter(servletRequest, servletResponse);
         
     }

     /**
      * 容器被销毁的时候被调用
      */
     @Override
     public void destroy() {
         System.out.println("拦截器销毁========拦截器销毁========");
     }

}
